﻿
using System;

namespace DailyFx.IndicatorExtension
{
    
	public class EMA : SMA
	{
 
		/// <summary>
		/// c'tor
		/// </summary>
		public EMA ( int span, int n) 
			: base ( span, n )
		{
		}

	
		public override void Cook ( BarSeries series )
		{
			decimal sum = 0.0m;
			decimal ma = 0.0m;

			for (int i = 0; i < this.Span; i++)
			{
				//
				//
				decimal c = series[i].LastPx;

				sum += c;
				ma = sum / (i + 1) ;
				this.MAimpl[i] = ma ;
			}

			decimal w = 2 / (this.Span+ 1);
			for (int n = this._span; n < Length; n++)   // forming
			{
				//
				decimal c = series[ n].LastPx;
				ma = ma + (c - ma) * w;

				this.MAimpl[ n ] = ma ;
			}
		}

		public override void Cook2 ( IVector target )
		{
			decimal sum = 0.0m;
			decimal ma = 0.0m;
			for (int i = 0; i < this.Span; i++)
			{
				//
				//
				decimal c = target[i];

				sum += c;
				ma = sum / (i + 1);
				this.MAimpl[i] = ma ;
			}

			decimal w = 2 / ( this.Span + 1);
			for (int n = this.Span; n < this.Length; n++)   // forming
			{
				//
				decimal c = target [n] ;
				ma = ma + (c - ma) * w;

				this.MAimpl[n] = ma ;
			}
		}


		public override string Family
		{
			get { return "Expnential Moving Average"; }
		}

		public override string Label
		{
			get { return String.Format( "EMA{0}", this.Span ); }
		}


	} // ks EMA

} // 
